package com.mes.web.controller.lz;

import com.mes.entity.lz.LzQueryCriteria;
import com.mes.entity.lz.TrolleyInfo;
import com.mes.entity.lz.TrolleyInfo_ProductCode;
import com.mes.entity.tdd.Staff;
import com.mes.service.lz.TrolleyInfoService;
import com.mes.utils.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName TrolleyInfoController
 * @Description 台车信息
 * @Author lz
 * @Date 2019年2月18日 14:50:37
 * @Version 1.0
 */
@Controller
@RequestMapping("/trolley")
public class TrolleyInfoController {
    public static final Integer AFFECT_ROW = 0;
    @Resource
    TrolleyInfoService trolleyInfoService;

    /**
     * 新增台车信息
     * @param response
     * @param trolleyInfo 台车实体
     */
    @RequestMapping("/addTrolleyInfo")
    public void addTrolleyInfo(HttpServletResponse response, HttpSession session, TrolleyInfo trolleyInfo) throws Exception {
        Staff staff = (Staff) session.getAttribute("staff");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        trolleyInfo.setRemark01(staff.getStaffname());
        trolleyInfo.setRemark02(sdf.format(new Date()));
        Long affectRow = trolleyInfoService.addTrolleyInfo(trolleyInfo);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 单个删除和批量删除
     * @param response
     * @param idArr id数组
     * @throws Exception
     */
    @RequestMapping("/deleteTrolleyInfoById")
    public void deleteTrolleyInfoById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr) throws Exception {
        for (int i = 0; i < idArr.length; i++) {
            Long affectRow = trolleyInfoService.deleteTrolleyInfoById(Long.parseLong(idArr[i]));
            if (affectRow > AFFECT_ROW) {
                ResponseUtil.write(response, affectRow);
            } else {
                ResponseUtil.write(response, AFFECT_ROW);
            }
        }
    }

    /**
     * 根据ID修改设备信息
     * @param response
     * @param trolleyInfo 选中行的台车
     * @throws Exception
     */
    @RequestMapping("/updateTrolleyInfoById")
    public void updateTrolleyInfoById(HttpServletResponse response, TrolleyInfo trolleyInfo) throws Exception {
        Long affectRow = trolleyInfoService.updateTrolleyInfoById(trolleyInfo);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 分页查询设备信息
     * @param response
     * @param criteria 分页查询条件
     */
    @RequestMapping("/queryTrolleyInfoList")
    public void queryTrolleyInfoList(HttpServletResponse response, LzQueryCriteria criteria, String trolleyName){
        if(trolleyName == null){
            trolleyName = "";
        }
        criteria.setKeyword01(trolleyName);
        List<TrolleyInfo> trolleyInfoList = trolleyInfoService.queryTrolleyInfoList(criteria);
        Long total = trolleyInfoService.countTrolleyInfoList(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(trolleyInfoList);
        jsonObject.put("rows", jsonArray);
        jsonObject.put("total", total);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据台车编码查询台车的状态
     * @param response
     * @param trolleyCode 台车编码
     */
    @RequestMapping("/queryTrolleyByTrolleyCode")
    public void queryTrolleyByTrolleyCode(HttpServletResponse response, String trolleyCode) throws Exception {
        List<TrolleyInfo> trolleyInfoList = trolleyInfoService.queryTrolleyByTrolleyCode(trolleyCode);
        if(trolleyInfoList.size() > 0){
            JSONArray jsonArray = JSONArray.fromObject(trolleyInfoList.get(0));
            ResponseUtil.write(response, jsonArray);
        } else {
            ResponseUtil.write(response, "");
        }
    }

    /**
     * 根据台车编码修改台车信息
     * @param response
     * @param trolleyCode 台车编码
     * @throws Exception
     */
    @RequestMapping("/updateTrolleyInfoStatusByTrolleyCode")
    public void updateTrolleyInfoStatusByTrolleyCode(HttpServletResponse response, String trolleyCode) throws Exception {
        Long affectRow = trolleyInfoService.updateTrolleyInfoStatusByTrolleyCode(trolleyCode);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 新增台车与产品编码绑定
     * @param response
     * @param trolleyInfo_ProductCode 台车与产品绑定实体
     * @throws Exception
     */
    @RequestMapping("/addTrolleyInfo_ProductCode")
    public void addTrolleyInfo_ProductCode(HttpServletResponse response, HttpSession session, TrolleyInfo_ProductCode trolleyInfo_ProductCode) throws Exception {
        Staff staff = (Staff) session.getAttribute("staff");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        trolleyInfo_ProductCode.setRemark01(staff.getStaffname());
        trolleyInfo_ProductCode.setRemark02(sdf.format(new Date()));
        Long affectRow = trolleyInfoService.addTrolleyInfo_ProductCode(trolleyInfo_ProductCode);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }
}
